package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.pms.entity.Project;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class ProjectDao {

	private DBHelp<Project> db=new DBHelp<Project>();

	public List<Project> findAllByEmployeeid(String employeeid){
		String sql="SELECT id,`name`,`desc`,createtime FROM t_project WHERE id IN(SELECT projectid FROM t_employee_project WHERE employeeid =?)";
		return db.executeQueryForList(sql,new ProjectRowMapper(),employeeid);
	}
	public void save(Project project){
		String sql="insert into t_project (id,`name`,`desc`,createtime) values(?,?,?,?)";
		db.executeSQL(sql, project.getId(),project.getName(),project.getDesc(),project.getCreatetime());
	}
	
	private class ProjectRowMapper implements RowMapper<Project>{

		@Override
		public Project mapperRow(ResultSet rs) throws SQLException {
			Project project = new Project();
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreatetime(rs.getString("createtime"));
			return project;
		}
	}

	public Project findProjectByProjectid(String projectid) {
		String sql="SELECT id,`name`,`desc`,createtime FROM t_project WHERE id IN(SELECT projectid FROM t_employee_project WHERE projectid=?)";
		return db.executeQueryForObject(sql,new ProjectRowMapper(), projectid);
	}
}
